﻿using InChatter.Client.Service;
using InChatter.Client.WPFClient.ViewModel;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace InChatter.Client.WPFClient.View
{
    /// <summary>
    /// MessageControl.xaml
    /// </summary>
    public partial class MessageControl : UserControl
    {
        MessageControlViewModel model;
        public MessageControl()
        {
            InitializeComponent();
            model = new MessageControlViewModel();
            this.DataContext = model;
        }

        #region LoginId Property

        public static DependencyProperty LoginIdProperty = DependencyProperty.Register("LoginId", typeof(string), typeof(MessageControl), new PropertyMetadata("", LoginIdChanged));

        public string LoginId
        {
            set
            {
                this.SetValue(LoginIdProperty, value);
            }
            get
            {
                return this.GetValue(LoginIdProperty).ToString();
            }
        }

        private static void LoginIdChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (ClientContext.AllClients == null || ClientContext.AllClients.Count == 0)
            {
                return;
            }
            if (e.NewValue == null)
            {
                ClientContext.Current = null;
            }
            else
            {
                ClientContext.Current = ClientContext.AllClients.FirstOrDefault(client => client.ClientId == e.NewValue.ToString());
            }
        }

        #endregion

        #region ClientsSource Property

        public static DependencyProperty ClientsSourceProperty = DependencyProperty.Register("ClientsSource", typeof(IEnumerable), typeof(MessageControl), new PropertyMetadata((IEnumerable)null, ClientsSourceChanged));

        public IEnumerable ClientsSource
        {
            set
            {
                this.SetValue(ClientsSourceProperty, value);
            }
            get
            {
                return this.GetValue(ClientsSourceProperty) as IEnumerable;
            }
        }

        private static void ClientsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ClientContext.AllClients.Clear();
            foreach (var item in e.NewValue as IEnumerable)
            {
                ClientContext.AllClients.Add((ClientContext)item);
            }
            MessageControl control = d as MessageControl;
            if (string.IsNullOrEmpty(control.LoginId))
            {
                return;
            }
            ClientContext.Current = ClientContext.AllClients.FirstOrDefault(client => client.ClientId == control.LoginId);
        }

        #endregion

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            MessagePoolWindow.Instance.Visibility = Visibility.Collapsed;
            MainWindow.Instance.Visibility = Visibility.Collapsed;
            if (ClientContext.Current.IsOnline)
            {
                string clients = ChatMsgCenter.Instance.GetOnlineClient();
                ChatMsgCenter.Instance.OnClientLogin(clients);
                if (string.IsNullOrEmpty(clients))
                {
                    ChatMsgCenter.Instance.SynchronousAllClients();
                }
                //Get messages that have not read;
                ChatMsgCenter.Instance.GetLocalUnReadMsg();
                //Get messages when client is log off;
                ChatMsgCenter.Instance.ReceiveLeaveMsg();
            }
        }
    }
}
